import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Scanner;

/**
 * @author Benny
 * 
 * */

public class Application {

	static boolean _serverIsRunning = true;

	public static void main(String[] args) {

		ServerSocket serverSocket;

		new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("server is running");
				Scanner sc = new Scanner(System.in);
				while (true) {
					String s = sc.next();
					if (s.equals("close"))
						_serverIsRunning = false;
					System.out.println("server closed ... ");
					break;

				}

			}
		}).start();

		try {
			serverSocket = new ServerSocket(8080);
			while (_serverIsRunning) {

				try {
					serverSocket.setSoTimeout(5000);
					Socket socket = serverSocket.accept();

					WebServer server = new WebServer(socket, new File(
					// "HtDocs"));
							"C:/Users/Benjamin/Desktop/HtDocs"));

					server.run();

					socket.close();
				} catch (SocketTimeoutException e) {
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
